package com.example.ayniu.treadmill.base;

/**
 * Created by ZXB on 2017/4/9.
 */

public class blueInterface {
    public final int MACRO_VALUE = 1000;
    public final int APP_OAM_HANDSHAKE_REQ = (MACRO_VALUE + 0x0001);
    public final int OAM_APP_HANDSHAKE_CFM = (MACRO_VALUE + 0x0002);
    public final int APP_OAM_START_REQ = (MACRO_VALUE + 0x0011);
    public final int OAM_APP_START_CFM = (MACRO_VALUE + 0x0012);
    public final int APP_OAM_STOP_REQ = (MACRO_VALUE + 0x0021);
    public final int OAM_APP_STOP_CFM = (MACRO_VALUE + 0x0022);
    public final int APP_OAM_PARA_GET_REQ = (MACRO_VALUE + 0x0031);
    public final int OAM_APP_PARA_GET_CFM = (MACRO_VALUE + 0x0032);
    public final int APP_OAM_SPEED_REQ = (MACRO_VALUE + 0x0041);
    public final int OAM_APP_SPEED_CFM = (MACRO_VALUE + 0x0042);
    int createHead(byte[] buffer, int msgType, int msgBodyLen) {
        buffer[0] = (byte)0x5a;
        buffer[1] = (byte)0xa5;
        buffer[2] = (byte)0x5a;
        buffer[3] = (byte)0xa5;
        buffer[8] = (byte)((msgType >>  8) & 0xFF);
        buffer[9] = (byte)(msgType & 0xFF);
        buffer[10] = (byte)((msgBodyLen >>  8) & 0xFF);
        buffer[11] = (byte)(msgBodyLen & 0xFF);
        return 16;
    };
    public class handShakeReq {
        public int createFrame(byte[] buffer) {
            int i;
            int headLen = createHead(buffer, APP_OAM_HANDSHAKE_REQ, 4);
            buffer[headLen + 0] = msgType;
            for(i = 0; i < 3; i++) {
                buffer[headLen + 1 + i] = usRes[i];
            }
            return (headLen + 4);
        };
        public byte msgType = 0;
        public byte[] usRes = new byte[3];
    };
    public class handShakeCfm {
        public int decodeFrame(byte[] buffer) {
            int i;
            deviceId = (buffer[16 + 0] << 24) & 0xFF000000;
            deviceId += (buffer[16 + 1] << 16) & 0xFF0000;
            deviceId += (buffer[16 + 2] << 8) & 0xFF00;
            deviceId += buffer[16 + 3] & 0xFF;
            for(i = 0; i < 32; i++) {
                version[i] = buffer[16 + 4 + i];
            }
            return 0;
        };
        public int deviceId;
        public byte[] version = new byte[32];
    }
    public class startReq {
        public int createFrame(byte[] buffer) {
            int i;
            int headLen = createHead(buffer, APP_OAM_START_REQ, 264 - 256);
            buffer[headLen + 0] = msgType;
            buffer[headLen + 1] = workType;
            buffer[headLen + 2] = targetType;
            buffer[headLen + 3] = usRes;
            buffer[headLen + 4] = (byte)((targetSet >> 24) & 0xFF);
            buffer[headLen + 5] = (byte)((targetSet >> 16) & 0xFF);
            buffer[headLen + 6] = (byte)((targetSet >> 8) & 0xFF);
            buffer[headLen + 7] = (byte)(targetSet & 0xFF);
            /*
            for(i = 0; i < 256; i++) {
                buffer[headLen + 8 + i] = courseName[i];
            }
            */
            return (headLen + 264 - 256);
        };
        public byte msgType;
        public byte workType;
        public byte targetType;
        public byte usRes;
        public int targetSet;
        public byte[] courseName = new byte[256];
    };
    public class startCfm {
        public int decodeFrame(byte[] buffer) {
            usResult = (buffer[16 + 0] << 24) & 0xFF000000;
            usResult += (buffer[16 + 1] << 16) & 0xFF0000;
            usResult += (buffer[16 + 2] << 8) & 0xFF00;
            usResult += buffer[16 + 3] & 0xFF;
            return 0;
        };
        public int usResult;
    }
    public class stopReq {
        public int createFrame(byte[] buffer) {
            int i;
            int headLen = createHead(buffer, APP_OAM_STOP_REQ, 4);
            buffer[headLen + 0] = msgType;
            for(i = 0; i < 3; i++) {
                buffer[headLen + 1 + i] = usRes[i];
            }
            return (headLen + 4);
        };
        public byte msgType;
        public byte[] usRes = new byte[3];
    };
    public class stopCfm {
        public int decodeFrame(byte[] buffer) {
            usResult = (buffer[16 + 0] << 24) & 0xFF000000;
            usResult += (buffer[16 + 1] << 16) & 0xFF0000;
            usResult += (buffer[16 + 2] << 8) & 0xFF00;
            usResult += buffer[16 + 3] & 0xFF;
            return 0;
        };
        public int usResult;
    }
    public class paraGetReq {
        public int createFrame(byte[] buffer) {
            int i;
            int headLen = createHead(buffer, APP_OAM_PARA_GET_REQ, 4);
            for(i = 0; i < 4; i++) {
                buffer[headLen + i] = usRes[i];
            }
            return (headLen + 4);
        };
        public byte[] usRes = new byte[4];
    };
    public class paraGetCfm {
        public int decodeFrame(byte[] buffer)
        {
            int i;
            status = buffer[16 + 0];
            workType = buffer[16 + 1];
            runTime = (short)(((buffer[16 + 2] << 8) & 0xFF00) + (buffer[16 + 3] & 0xFF));
            speed = (short)(((buffer[16 + 4] << 8) & 0xFF00) + (buffer[16 + 5] & 0xFF));
            speedLevel = (short)(((buffer[16 + 6] << 8) & 0xFF00) + (buffer[16 + 7] & 0xFF));
            distance = (int)((buffer[16 + 8] << 24) & 0xFF000000);
            distance += (int)((buffer[16 + 9] << 16) & 0xFF0000);
            distance += (int)((buffer[16 + 10] << 8) & 0xFF00);
            distance += (int)((buffer[16 + 11]) & 0xFF);
            calorie = (int)((buffer[16 + 12] << 24) & 0xFF000000);
            calorie += (int)((buffer[16 + 13] << 16) & 0xFF0000);
            calorie += (int)((buffer[16 + 14] << 8) & 0xFF00);
            calorie += (int)((buffer[16 + 15]) & 0xFF);
            targetSet = (int)((buffer[16 + 16] << 24) & 0xFF000000);
            targetSet += (int)((buffer[16 + 17] << 16) & 0xFF0000);
            targetSet += (int)((buffer[16 + 18] << 8) & 0xFF00);
            targetSet += (int)((buffer[16 + 19]) & 0xFF);
            return 0;
        };
        public byte status;
        public byte workType;
        public short runTime;
        public short speed;
        public short speedLevel;
        public int distance;
        public int calorie;
        public int targetSet;
    }
    public class speedReq {
        public int createFrame(byte[] buffer) {
            int headLen = createHead(buffer, APP_OAM_SPEED_REQ, 4);
            buffer[headLen + 0] = type;
            buffer[headLen + 1] = (byte)((speedLev >> 8) & 0xFF);
            buffer[headLen + 2] = (byte)(speedLev & 0xFF);
            buffer[headLen + 3] = usRes;
            return (headLen + 4);
        };
        public byte type;
        public short speedLev;
        public byte usRes;
    };
    public class speedCfm {
        public int decodeFrame(byte[] buffer) {
            speedLev = (buffer[16 + 0] << 24) & 0xFF000000;
            speedLev += (buffer[16 + 1] << 16) & 0xFF0000;
            speedLev += (buffer[16 + 2] << 8) & 0xFF00;
            speedLev += buffer[16 + 3] & 0xFF;
            return 0;
        };
        public int speedLev;
    }
}



